package com.xwz.fullstack.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xwz.fullstack.entity.LeaveStudy;
import com.xwz.fullstack.entity.Meeting;
import com.xwz.fullstack.common.Result;
import com.xwz.fullstack.mapper.MeetingMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XiaoWenzhuo
 * @since 2021-11-15
 */
@Slf4j
@RestController
@RequestMapping("/meeting")
public class MeetingController {
    @Resource
    MeetingMapper meetingMapper;

    @PostMapping()
    public Result<?> add(@RequestBody Meeting meeting){
        meeting.setDateWeek(DateUtil.weekOfYear(DateUtil.date()));
        meeting.setDateNow(DateUtil.date());
        LambdaQueryWrapper<Meeting> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Meeting::getNameStudent,meeting.getNameStudent());
        wrapper.eq(Meeting::getDateWeek,meeting.getDateWeek());
        List<Meeting> meetings = meetingMapper.selectList(wrapper);
        meeting.setMeetingNumber(String.valueOf(meetings.size()+1));
        meetingMapper.insert(meeting);
        log.info("会议请假新增------》 " + meeting.toString());
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Meeting meeting) {
        meetingMapper.updateById(meeting);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public  Result<?> deleteById(@PathVariable int id) {
        meetingMapper.deleteById(id);
        log.warn("会议请假删除------》deleteById(" + id + ")");
        return Result.success();
    }

    @GetMapping("/findAll")
    public Result<?> findAll() {
        QueryWrapper<Meeting> queryWrapper = new QueryWrapper<>();

        List<Meeting> list = meetingMapper.selectList(queryWrapper);
        return Result.success(list);
    }
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Meeting> wrapper = Wrappers.<Meeting>lambdaQuery();
        wrapper.orderByDesc(Meeting::getId);
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Meeting::getDateMeeting, search);
        }
        Page<Meeting> userPage = meetingMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }

}

